package com.zhkj.txg.utils

import android.view.View
import androidx.viewpager.widget.ViewPager
import kotlin.math.abs

class ViewPagerTransformer : ViewPager.PageTransformer {

    private val SCALE_MAX = 0.8

    override fun transformPage(page: View, position: Float) {
        val parent = page.parent
        if (parent is ViewPager) {
            val clientWidth = parent.measuredWidth - parent.paddingLeft - parent.paddingRight
            val childLeft = position * clientWidth + parent.scrollX
            val newPosition = (childLeft - parent.paddingLeft - parent.scrollX) / clientWidth
            val scale = (SCALE_MAX - 1) * abs(newPosition) + 1
            if (newPosition < 0) {
                page.pivotX = page.width.toFloat()
                page.pivotY = (page.height / 2).toFloat()
            } else {
                page.pivotX = 0F
                page.pivotY = (page.height / 2).toFloat()
            }
            page.scaleX = scale.toFloat()
            page.scaleY = scale.toFloat()
        }
    }
}